package com.lagou;

import com.lagou.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Random;
import java.util.concurrent.*;

public class ConsumerApplication {
    public static void main(String[] args) throws  Exception{
        ClassPathXmlApplicationContext applicationContext  = new ClassPathXmlApplicationContext("classpath:dubbo-comsumer.xml");
        //applicationContext.start();
        HelloService  helloService  = applicationContext.getBean("helloService",HelloService.class);


        ExecutorService pool = new ThreadPoolExecutor(100, 500, 100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

            }
        });

        for (int i=0 ; i < 5000; i++){

            pool.execute(new Runnable(){
                @Override
                public void run(){
                    String result1 = helloService.sayHello("hello");
                    try {
                        TimeUnit.SECONDS.sleep(new Random().nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String result2 = helloService.sayGoodbye("Goodbye");
                    try {
                        Thread.sleep(new Random().nextInt(500));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String result3 = helloService.saySeeYou("SeeYou");

                }
            });


        }
        System.in.read();
    }
}
